package com.tyz.hellocst.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import com.tyz.hellocst.AppContext;
import com.tyz.hellocst.R;
import com.tyz.hellocst.adapter.GoodsItemAdapter;
import com.tyz.hellocst.database.DBOperate;
import com.tyz.hellocst.entity.Goods;

import java.util.ArrayList;

public class GoodsListActivity extends AppCompatActivity {

    private TextView tvTitle;
    private ListView listView;
    private ArrayList<Goods> arrayList;

    private String intentName;
    private String intentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);

        Intent intent = getIntent();
        //列表名字
        intentName = intent.getStringExtra("intentName");
        //加载列表选项
        intentPosition = intent.getStringExtra("intentPosition");

        initID();
        initListView();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String intentGoodsId = (arrayList.get(position)).getGoodsId();

                Intent intent = new Intent(GoodsListActivity.this, GoodsActivity.class);
                intent.putExtra("intentName", intentName);//保留
                intent.putExtra("intentPosition", intentPosition);//保留
                intent.putExtra("intentGoodsId", intentGoodsId);
                startActivity(intent);
                finish();

            }
        });
    }//onCreate

    private void initID() {
        tvTitle = findViewById(R.id.goods_list_title);
        listView = findViewById(R.id.goods_list_listView);
    }

    public void initListView() {
        tvTitle.setText(intentName + ":");

        arrayList = new ArrayList<>();
        ArrayAdapter<Goods> adapter = new GoodsItemAdapter(this, arrayList);
        listView.setAdapter(adapter);

        DBOperate dbOperate = new DBOperate(this);
        dbOperate.openDB();
        SQLiteDatabase db = dbOperate.getDb();

        String selection = "";
        String[] selectionArgs = new String[1];
        switch (Integer.parseInt(intentPosition)) {
            case 1:
                //已发布
                selection = "goodsOwner=? and goodsState=0";
                selectionArgs = new String[]{AppContext.getUserName()};
                break;
            case 2:
                //已下单
                selection = "goodsBuyers=? and goodsState=1";
                selectionArgs = new String[]{AppContext.getUserName()};
                break;
            case 3:
                //订单
                selection = "goodsOwner=? and goodsState=1";
                selectionArgs = new String[]{AppContext.getUserName()};
                break;
            case 4:
                //交易中
                selection = "(goodsOwner=? or goodsBuyers=?) and goodsState=2";
                selectionArgs = new String[]{AppContext.getUserName(), AppContext.getUserName()};
                break;
            case 5:
                //交易完成
                selection = "(goodsOwner=? or goodsBuyers=?) and goodsState=3";
                selectionArgs = new String[]{AppContext.getUserName(), AppContext.getUserName()};
                break;
            case 6:
                //已下架
                selection = "goodsOwner=? and goodsState=4";
                selectionArgs = new String[]{AppContext.getUserName()};
                break;
            case 7:
                //取消交易
                selection = "(goodsOwner=? or goodsBuyers=?) and goodsState=5";
                selectionArgs = new String[]{AppContext.getUserName(),AppContext.getUserName()};
                break;
            default:
        }
        //asc升序,DESC逆序
        Cursor cursor = db.query(dbOperate.getGoodsTableName(),
                null, selection, selectionArgs,
                null, null, "goodsTime DESC");

        arrayList.clear();
        if (cursor.getCount() != 0) {
            cursor.moveToFirst();
            do {
                int bytesIndex =cursor.getColumnIndex("goodsImage");
                byte[] bytes = cursor.getBlob(bytesIndex);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

                Goods goods = new Goods();
                int GoodsIdIndex = cursor.getColumnIndex("goodsId");
                goods.setGoodsId(cursor.getString(GoodsIdIndex));
                int GoodsNameIndex = cursor.getColumnIndex("goodsName");
                goods.setGoodsName(cursor.getString(GoodsNameIndex));
                int GoodsTextIndex = cursor.getColumnIndex("goodsText");
                goods.setGoodsText(cursor.getString(GoodsTextIndex));
                goods.setGoodsImage(bitmap);
                int GoodsTimeIndex = cursor.getColumnIndex("goodsTime");
                goods.setGoodsTime(cursor.getString(GoodsTimeIndex));
                int GoodsPriceIndex = cursor.getColumnIndex("goodsPrice");
                goods.setGoodsPrice(cursor.getDouble(GoodsPriceIndex));
                int GoodsOwnerIndex = cursor.getColumnIndex("goodsOwner");
                goods.setGoodsOwner(cursor.getString(GoodsOwnerIndex));
                int GoodsOwnerContactIndex = cursor.getColumnIndex("goodsOwnerContact");
                goods.setGoodsOwnerContact(cursor.getString(GoodsOwnerContactIndex));
                int GoodsStateIndex = cursor.getColumnIndex("goodsState");
                goods.setGoodsState(cursor.getInt(GoodsStateIndex));
                int GoodsBuyersIndex = cursor.getColumnIndex("goodsBuyers");
                goods.setGoodsBuyers(cursor.getString(GoodsBuyersIndex));

                arrayList.add(goods);
                adapter.notifyDataSetChanged();//刷新数据
            } while (cursor.moveToNext());

        }//if
        dbOperate.closeDb();
        cursor.close();
    }


}
